﻿using System;
using System.Data;
using Model;
using Service;
using Resources;


public partial class ProblemView : System.Web.UI.Page
{
    private DirectServiceProblem _DirectServiceProblem = new DirectServiceProblem();
    private StockServiceProblem _stockServiceProblem = new StockServiceProblem();
    protected void Page_Load(object sender, EventArgs e)
    {
        new Utility().SetRowToDisplay(grdProblem, Session["rowsToSDisplay"]);
        ValueUseOtherPage.boolProblem = false; // Parameter for dont display data again at page Modify. 
        txtHiddenSoftwareId.Text = Request.QueryString["problemId"];
        if (Session["whereClause"] == null)
            Session["whereClause"] = txtHiddenWhereClause.Text;
        if (Session["orderByClause"] == null)
            Session["orderByClause"] = txthiddenOrderbyClause.Text;
       
        
        
        if (!IsPostBack)
        {
            // to avoid loading for the first time
            Session["problemId"] = Request.QueryString["problemId"];
            Session["defaultTabIndex"] = Request.QueryString["tabIndex"];

            if ((Request.QueryString["detail"] == null))
            {

                if ((Request.QueryString["delete"] == null))
                {

                }
                else
                {
                    int problemId = Request.QueryString["problemId"] != null ? Int32.Parse(Request.QueryString["problemId"]) : 0;

                    //_stockServiceProblem.DeleteProblem(problemId);



                    if (!new Utility().TestPermissions(Resource.PermissionDeleteProblem))
                    {
                        lblMessageValidate.Text = Resource.Permission;
                        return;
                    }

                    _DirectServiceProblem.DeteteProblem(problemId);

                    lblTotalProblem.Text = "Total problem : " +new DirectServiceProblem().GetProblemDisplayed(GetWhereClause(), GetOrderClause()).Rows.Count;
     
                }
            }


            if (Session["pageIndex"] != null)
            {
                grdProblem.PageIndex = Int32.Parse(Session["pageIndex"].ToString());
            }

            /* 
             * 1. number of equipments
             * 2. select the element 
             * */


        }
        else
        {
            //  SystemList = new DirectService().DataTableToIlistSystem(GetWhereClause(), GetOrderClause());
            txtHiddenPageIndex.Text = "0";
            lblTotalProblem.Text = "Total supplier : " + new DirectServiceProblem().GetProblemDisplayed(GetWhereClause(), GetOrderClause()).Rows.Count;
            int number = +new DirectServiceProblem().GetProblemDisplayed(GetWhereClause(), GetOrderClause()).Rows.Count;
            if (number == 0)
            {
                grdProblem1.Visible = true;
            }
            else
            {
                grdProblem.Visible = true;
                grdProblem1.Visible = false;
            }
        }
        grdProblem.PageIndex = !String.IsNullOrEmpty(txtHiddenPageIndex.Text) ? Int32.Parse(txtHiddenPageIndex.Text) : 0;
    }
    protected void bntFilter_Click(object sender, EventArgs e)
    {
        Session["whereClause"] = GetWhereClause();
        Session["orderByClause"] = GetOrderClause();
        txthiddenOrderbyClause.Text = GetOrderClause();
        txtHiddenWhereClause.Text = GetWhereClause();
        Session["defaultTabIndex"] = 0;
        ClearControlAdd();
    }
    private void ClearControlAdd()
    {
        txtProblemId.Text = "";
        txtProblem.Text = ""; 
    }
    public int GetProblemId()
    {
        return !String.IsNullOrEmpty(Request.QueryString["problemId"]) ? Int32.Parse(Request.QueryString["problemId"]) : 0;
    }

   


    public int GetTabIndex()
    {
        int tabIndex = Request.QueryString["tabIndex"] != null
                        ?
                            Int32.Parse(Request.QueryString["tabIndex"])
                        : 0;
        if (Session["defaultTabIndex"] != null)
        {
            tabIndex = Int32.Parse(Session["defaultTabIndex"].ToString());
            Session["defaultTabIndex"] = null;
        }

        return tabIndex;
    }

    public int GetPageIndex()
    {
        return Session["pageIndex"] != null
                   ? Int32.Parse(Session["pageIndex"].ToString()) : 0;
    }
    

    protected string GetWhereClause()
    {
        string whereClause = string.Empty;
        string and = "";
        string str = "";

        // Prepare by equipement
        int i = 0;

        if (!string.IsNullOrEmpty(txtProblemId.Text))
        {
            if (int.TryParse(txtProblemId.Text, out i))
            {
                and = " and ";
                str = " where Problem.ProblemId=" + Int32.Parse(txtProblemId.Text);
                if (txtProblem.Text != string.Empty)
                {
                    str += " " + "and" + " " + "Problem.Problem=" + "'" + txtProblem.Text + "'";
                    and = " and ";
                }
            }
            else
            {
                grdProblem.Visible = false;
                grdProblem1.Visible = true;
                lblTotalProblem.Text = "Total problem : 0";
                return null;
            }
        }
        else
        {
            if (txtProblem.Text != string.Empty)
            {
                str += " " + "where" + " " + "Problem.Problem like" + "'%" + txtProblem.Text + "%'";
                and = " and ";
            }
        }

        if (!String.IsNullOrEmpty(str))
            whereClause = str + " ";
        return whereClause;
    }
    protected string GetOrderClause()
    {
        return " Order by Problem.Problem";
    }

    protected void grdProblem_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Delete"))
        {
            int problemId = Convert.ToInt32(grdProblem.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
            _DirectServiceProblem.DeteteProblem(problemId);
            Response.Redirect("ProblemView.aspx");
        }
        if (e.CommandName.Equals("Select"))
        {
           // int problemId = Convert.ToInt32(grdProblem.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
           // Session["problemId"] = problemId;
            Session["defaultTabIndex"] = 1;
            Session["rowSelectedIndex"] = Convert.ToInt32(e.CommandArgument);
        }


        new Utility().SetSelectedColorToGridView(e, grdProblem);
    }
    protected void dsProblem_Selected(object sender, System.Web.UI.WebControls.ObjectDataSourceStatusEventArgs e)
    {
       // if (e.ReturnValue != null) lblTotalProblem.Text = "Total problem:" + ((DataTable)e.ReturnValue).Rows.Count;

        lblTotalProblem.Text = "Total plannings : " +
                                     (e.ReturnValue != null
                                          ? ((DataTable)e.ReturnValue).Rows.Count.ToString()
                                          : 0.ToString());

        grdProblem1.Visible = (e.ReturnValue == null
                                      ? true
                                      : ((DataTable)e.ReturnValue).Rows.Count == 0 ? true : false); 

    }
    protected void grdProblem_PageIndexChanged(object sender, EventArgs e)
    {

    }
    protected void grdProblem_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void grdProblem_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {


    }
    protected void grdProblem_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
    {

    }
}
